package leetcode.editor.cn;

import java.util.*;

public class P447NumberOfBoomerangs {
    public static void main(String[] args) {
        Solution solution = new P447NumberOfBoomerangs().new Solution();
        System.out.println(solution.numberOfBoomerangs(new int[][]{{0, 0}, {1, 0}, {2, 0}}));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int numberOfBoomerangs(int[][] points) {
            int ans = 0;
            for (int[] p : points) {
                Map<Integer, Integer> cnt = new HashMap<Integer, Integer>();
                for (int[] q : points) {
                    int dis = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]);
                    cnt.put(dis, cnt.getOrDefault(dis, 0) + 1);
                }
                for (Map.Entry<Integer, Integer> entry : cnt.entrySet()) {
                    int m = entry.getValue();
                    ans += m * (m - 1);
                }
            }
            return ans;
        }

        public int numberOfBoomerangsMy(int[][] points) {
            int len = points.length;
            if (len < 3) {
                return 0;
            }
            int count = 0;
            Map<Integer, Map<int[], Integer>> map = new HashMap<>();
            for (int i = 0; i < len; i++) {
                int[] start = points[i];
                for (int j = i + 1; j < len; j++) {
                    int[] end = points[j];
                    int x1 = start[0];
                    int x3 = end[0];
                    int y1 = start[1];
                    int y3 = end[1];
                    int distance = (x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1);
                    map.compute(distance, (integer, nodeListMap) -> {
                        if (nodeListMap == null) {
                            nodeListMap = new HashMap<>();
                        }
                        nodeListMap.compute(start, (node, nodes) -> nodes == null ? 1 : nodes + 1);
                        nodeListMap.compute(end, (node, nodes) -> nodes == null ? 1 : nodes + 1);
                        return nodeListMap;
                    });
                }
            }
            for (Map<int[], Integer> value : map.values()) {
                for (Integer size : value.values()) {
                    count += size * (size - 1);
                }
            }
            return count;
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}